package app.agent.systems

import app.comet.{RequestSystems, Subscriber}
import app.ui.widget.ExpandingToggleParent
import im.mange.jetboot.js.JsCmdFactory
import im.mange.jetboot.{R, Composite, Renderable, Html}
import JsCmdFactory._
import SystemsLocator._
import Html._
import app.ui.widget.ExpandingToggle
import app.server.SystemsResponse

//TODO: slide in the content
//TODO: support remove
//TODO: support disable
//TODO: show count e.g Apps badges
//TODO: show alerts badges if problem with any app discovery refresh
case class SystemsAgent(subscriber: Subscriber) extends Renderable {
  private val contentAgent = SystemsContentAgent(subscriber)
  private val content = span(R("Systems"))

  private val toggle = ExpandingToggle(systemsToggle, content, "cog", new ExpandingToggleParent {
    override def onShow = showSystems
    override def onHide = contentAgent.onHide //TODO: this looks common
  })

  //TODO: should be div?
  override def render = Composite(toggle, contentAgent).render

  def onShowResponse(response: SystemsResponse) = toggle.stopSpinner & contentAgent.onShowResponse(response)

  //TODO: should this go upto parent.xyz instead?
  private[agent] def showSystems = {
    subscriber ! RequestSystems
    nothing
  }
}
